package com.copro.chemdb.constants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.db.sql.SQLSegment;

import com.copro.simple.Application;
import com.copro.simple.SimpleException;
import com.copro.simple.db.QueryCallback;

public class ConstValueUtils {

	static class ListGetter implements QueryCallback {

		private List<ConstValue> list;

		public ListGetter(List<ConstValue> list) {
			this.list = list;
		}

		public List<ConstValue> getList() {
			return this.list;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			while (rs.next()) {
				list.add(new ConstValue(rs));
			}
		}
	}

	static class DetailGetter implements QueryCallback {

		private ConstValue constValue;

		public DetailGetter(ConstValue ConstValue) {
			this.constValue = ConstValue;
		}

		public ConstValue getObject() {
			return this.constValue;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			if (rs.first())
				this.constValue = new ConstValue(rs);

		}

	}

	public static List<ConstValue> getConstValueList(Application app, int typeId)
			throws SimpleException {
		ListGetter callback = new ListGetter(new LinkedList<ConstValue>());
		SQLSegment sql = app.getSQL("getConstValueList");
		sql.set("id_L_type", typeId <= 0 ? "NULL" : typeId);
		app.query(sql.toString(), callback);
		return callback.getList();
	}

	public static ConstValue getConstValue(Application app, int type, int id)
			throws SimpleException {
		DetailGetter callback = new DetailGetter(new ConstValue());
		SQLSegment sql = app.getSQL("getConstValue");
		sql.set("id", id);
		sql.set("type", type);
		app.query(sql.toString(), callback);
		return callback.getObject();
	}

}
